<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yang-da-xiong.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="前言编译原理课程实验的实验课内容—构造自顶向下的语法分析程序。通过本次实验，可以熟练掌握对于LL(1)分析表的构造方法。">
<meta property="og:type" content="article">
<meta property="og:title" content="编译原理--实验2 语法分析">
<meta property="og:url" content="https://yang-da-xiong.gitee.io/2022/01/13/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86--%E5%AE%9E%E9%AA%8C2%20%E8%AF%AD%E6%B3%95%E5%88%86%E6%9E%90/index.html">
<meta property="og:site_name" content="杨大熊的代码世界">
<meta property="og:description" content="前言编译原理课程实验的实验课内容—构造自顶向下的语法分析程序。通过本次实验，可以熟练掌握对于LL(1)分析表的构造方法。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/34014646b7dc4928b6a72d71a27d8500.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/dfe4bd754c8f41d4b226f3b557b4ff5d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_10,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2d4f0c80588748bda6764ae368bd69d8.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/94f1b2dbf8054541b79ae1662bf749a5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_9,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2b15f1a732304ea0a7b0519b2981ba02.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_16,color_FFFFFF,t_70,g_se,x_16">
<meta property="article:published_time" content="2022-01-13T04:40:48.328Z">
<meta property="article:modified_time" content="2022-01-13T04:47:16.958Z">
<meta property="article:author" content="杨大熊">
<meta property="article:tag" content="编译原理">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/34014646b7dc4928b6a72d71a27d8500.png">

<link rel="canonical" href="https://yang-da-xiong.gitee.io/2022/01/13/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86--%E5%AE%9E%E9%AA%8C2%20%E8%AF%AD%E6%B3%95%E5%88%86%E6%9E%90/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>编译原理--实验2 语法分析 | 杨大熊的代码世界</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">杨大熊的代码世界</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://yang-da-xiong.gitee.io/2022/01/13/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86--%E5%AE%9E%E9%AA%8C2%20%E8%AF%AD%E6%B3%95%E5%88%86%E6%9E%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="杨大熊">
      <meta itemprop="description" content="各类IT知识分享">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="杨大熊的代码世界">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          编译原理--实验2 语法分析
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2022-01-13 12:40:48 / 修改时间：12:47:16" itemprop="dateCreated datePublished" datetime="2022-01-13T12:40:48+08:00">2022-01-13</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/" itemprop="url" rel="index"><span itemprop="name">编译原理</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>编译原理课程实验的实验课内容—构造自顶向下的语法分析程序。通过本次实验，可以熟练掌握对于LL(1)分析表的构造方法。</p>
<span id="more"></span>

<h1 id="1-1实验目的"><a href="#1-1实验目的" class="headerlink" title="1.1实验目的"></a>1.1实验目的</h1><p>（1）熟练掌握 LL(1)分析表的构造方法。 </p>
<p>（2）掌握设计、编制和调试典型的语法分析程序，进一步掌握常用的语法分析方法。 </p>
<h1 id="1-2-实验任务"><a href="#1-2-实验任务" class="headerlink" title="1.2 实验任务"></a>1.2 实验任务</h1><p>根据 LL(1)分析法自己编写一个语法分析程序，语言不限，文法不限。 </p>
<h1 id="1-3-实验内容"><a href="#1-3-实验内容" class="headerlink" title="1.3 实验内容"></a>1.3 实验内容</h1><h2 id="1-3-1-实验要求"><a href="#1-3-1-实验要求" class="headerlink" title="1.3.1 实验要求"></a>1.3.1 实验要求</h2><p>你的程序应具有通用性，能够识别由词法分析得出的词法单元序列是否是给定文法的正 确句子（程序），并能够输出分析过程和识别结果。 </p>
<h2 id="1-3-2-输入格式"><a href="#1-3-2-输入格式" class="headerlink" title="1.3.2 输入格式"></a>1.3.2 输入格式</h2><p>输入一行源代码，通过一次扫描完成语法分析。</p>
<h2 id="1-3-3-输出格式"><a href="#1-3-3-输出格式" class="headerlink" title="1.3.3 输出格式"></a>1.3.3 输出格式</h2><p>实验要求通过标准输出打印程序的运行结果（包括 First 集、Follow 集、LL(1)分析表）， 此外，要求可以保存 LL(1)分析表。 你的程序需要输出语法分析过程和相应的分析结果（即此串是否为 LL(1)文法的句子）。 </p>
<h2 id="1-3-4-样例"><a href="#1-3-4-样例" class="headerlink" title="1.3.4 样例"></a>1.3.4 样例</h2><p>输入示例1：</p>
<p><img src="https://img-blog.csdnimg.cn/34014646b7dc4928b6a72d71a27d8500.png" alt="在这里插入图片描述"></p>
<p>输出示例1：</p>
<p><img src="https://img-blog.csdnimg.cn/dfe4bd754c8f41d4b226f3b557b4ff5d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_10,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<p>输入示例2：</p>
<p><img src="https://img-blog.csdnimg.cn/2d4f0c80588748bda6764ae368bd69d8.png" alt="在这里插入图片描述"></p>
<p>输出示例2：</p>
<p><img src="https://img-blog.csdnimg.cn/94f1b2dbf8054541b79ae1662bf749a5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_9,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h1 id="1-4-程序"><a href="#1-4-程序" class="headerlink" title="1.4 程序"></a>1.4 程序</h1><h2 id="1-4-1-程序流程图"><a href="#1-4-1-程序流程图" class="headerlink" title="1.4.1 程序流程图"></a>1.4.1 程序流程图</h2><p><img src="https://img-blog.csdnimg.cn/2b15f1a732304ea0a7b0519b2981ba02.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_16,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h2 id="1-4-2-算法描述"><a href="#1-4-2-算法描述" class="headerlink" title="1.4.2 算法描述"></a>1.4.2 算法描述</h2><p>本 LL（1）文法分析程序只针对一般的 LL（1）文法，不能将一个很 复杂的非 LL（1）文法转化为 LL（1）文法，因为太复杂了，时间也不太 够，本人目前无法实现，于是也不可以分析 C—语言文法，这里只针对以 大写字母表示非终结符的文法进行分析。 另外，因为 Visual Studio 无法正常打印ε这个代表空的字符，于是这 里采用另一套标准：即$代表一个空串，#代表一个空字符。</p>
<p>​    程序的整体设计思路上，通过几个函数来分别实现计算并输出first集，计算并输出follow集，保存并输出LL(1)分析表，最后利用分析表求一个给定输入串的分析过程。</p>
<p>​    首先说说First集合的求法，大致的思路就是先从右部的第一个文法符号开始，如果是终结符直接加入first集并结束，如果是非终结符，那么就将该非终结符作为新的参数，递归调用求first的函数，直到遇到终结符。</p>
<p>​    对于follow集，首先看该符号后面是终结符还是非终结符还是在末尾，对于终结符，直接将其假如follow集中，如果是非终结符，那么要去求非终结符的first，如果是末尾，那么要用到左部非终结符的follow集，并通过几次不断的重复调用follow集函数，来求完整的follow集（也就是所谓的当follow集不再增加时）。</p>
<p>​    对于输入符号串求分析表的过程，我们可以通过建立二维数组的方式，存入LL（1）分析表，然后在分析过程中，对于指针指向的符号，根据分析栈的栈顶信息，在分析表中通过行和列判断是移入文法还是进行匹配操作，同时输出整个分析过程。</p>
<p>​    <strong>本程序采用直接输入根据已知文法构造的分析表 M。</strong></p>
<h2 id="1-4-3-程序源码"><a href="#1-4-3-程序源码" class="headerlink" title="1.4.3 程序源码"></a>1.4.3 程序源码</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;Windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//表格数组   (所使用的文法为教材P93页不含左递归的样例文法)</span></span><br><span class="line"><span class="comment">//                          i       +       *      (        )      #</span></span><br><span class="line"><span class="keyword">char</span> LL1[<span class="number">50</span>][<span class="number">50</span>][<span class="number">100</span>] = &#123; &#123;<span class="string">&quot;-&gt;TM&quot;</span>,<span class="string">&quot;null&quot;</span> ,<span class="string">&quot;null&quot;</span>,<span class="string">&quot;-&gt;TM&quot;</span> ,<span class="string">&quot;null&quot;</span>,<span class="string">&quot;null&quot;</span> &#125;,  <span class="comment">//一个容量是100个字符的字符串，总共50行，50列</span></span><br><span class="line">						 &#123;<span class="string">&quot;null&quot;</span>,<span class="string">&quot;-&gt;+TM&quot;</span> ,<span class="string">&quot;null&quot;</span>,<span class="string">&quot;null&quot;</span> ,<span class="string">&quot;-&gt;$&quot;</span>, <span class="string">&quot;-&gt;$&quot;</span>&#125;,</span><br><span class="line">						 &#123;<span class="string">&quot;-&gt;FN&quot;</span>,<span class="string">&quot;null&quot;</span> ,<span class="string">&quot;null&quot;</span> ,<span class="string">&quot;-&gt;FN&quot;</span>,<span class="string">&quot;null&quot;</span>,<span class="string">&quot;null&quot;</span>  &#125;,</span><br><span class="line">						 &#123;<span class="string">&quot;null&quot;</span>,<span class="string">&quot;-&gt;$&quot;</span> ,<span class="string">&quot;-&gt;*FN&quot;</span> ,<span class="string">&quot;null&quot;</span>  ,<span class="string">&quot;-&gt;$&quot;</span>,<span class="string">&quot;-&gt;$&quot;</span>  &#125;,</span><br><span class="line">						 &#123;<span class="string">&quot;-&gt;i&quot;</span> ,<span class="string">&quot;null&quot;</span>,<span class="string">&quot;null&quot;</span>,<span class="string">&quot;-&gt;(E)&quot;</span> ,<span class="string">&quot;null&quot;</span>,<span class="string">&quot;null&quot;</span> &#125; &#125;;</span><br><span class="line"><span class="keyword">char</span> H[<span class="number">200</span>] = <span class="string">&quot;EMTNF&quot;</span>;</span><br><span class="line"><span class="keyword">char</span> L[<span class="number">200</span>] = <span class="string">&quot;i+*()#&quot;</span>;</span><br><span class="line"><span class="built_in">stack</span>&lt;<span class="keyword">char</span>&gt;cmp;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">findH</span><span class="params">(<span class="keyword">char</span> a)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) <span class="comment">//找到对应的非终结符</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (a == H[i])</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">return</span> i;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">findL</span><span class="params">(<span class="keyword">char</span> b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">6</span>; i++) <span class="comment">//找到对应的终结符</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (b == L[i])</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">return</span> i;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">error</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">int</span> cnt, <span class="keyword">int</span> len, <span class="keyword">char</span> p[], <span class="keyword">char</span> str[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\t%s\t&quot;</span>, cnt, p);</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> q = i; q &lt; len; q++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; str[q];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\t报错\n&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> len;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">analyze</span><span class="params">(<span class="keyword">char</span> str[], <span class="keyword">int</span> len)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> cnt = <span class="number">1</span>; <span class="comment">//输出Step专用</span></span><br><span class="line">	<span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">char</span> p[<span class="number">200</span>] = <span class="string">&quot;#E&quot;</span>; <span class="comment">//输出stack专用，即输出分析栈</span></span><br><span class="line">	<span class="keyword">int</span> pindex = <span class="number">2</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;Step\tStack\tString\tRule\n&quot;</span>);</span><br><span class="line">	<span class="keyword">while</span> (i &lt; len)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> x, y;</span><br><span class="line">		<span class="keyword">char</span> ch = cmp.top();  <span class="comment">//cmp是分析状态栈，ch是栈顶符号</span></span><br><span class="line">		<span class="keyword">if</span> (ch &gt;= <span class="string">&#x27;A&#x27;</span>&amp;&amp;ch &lt;= <span class="string">&#x27;Z&#x27;</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			cmp.pop();  <span class="comment">//出栈</span></span><br><span class="line">			x = findH(ch);   <span class="comment">//x是对应的非终结符位置</span></span><br><span class="line">			y = findL(str[i]);  <span class="comment">//y是对应的终结符位置</span></span><br><span class="line">			<span class="keyword">if</span> (x != <span class="number">-1</span> &amp;&amp; y != <span class="number">-1</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">int</span> len2 = <span class="built_in">strlen</span>(LL1[x][y]);  <span class="comment">//记录这个字符串的长度</span></span><br><span class="line">				<span class="keyword">if</span> (<span class="built_in">strcmp</span>(LL1[x][y], <span class="string">&quot;null&quot;</span>) == <span class="number">0</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					i = error(i, cnt, len, p, str);</span><br><span class="line">					<span class="keyword">continue</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;%d\t%s\t&quot;</span>, cnt, p);  <span class="comment">//输出状态栈信息</span></span><br><span class="line">				<span class="keyword">if</span> (p[pindex - <span class="number">1</span>] != <span class="string">&#x27;#&#x27;</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					p[pindex] = <span class="string">&#x27;\0&#x27;</span>;  <span class="comment">//字符串结束标志</span></span><br><span class="line">					pindex--;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">if</span> (LL1[x][y][<span class="number">2</span>] != <span class="string">&#x27;$&#x27;</span>)  <span class="comment">//如果文法不是推出空</span></span><br><span class="line">				&#123;</span><br><span class="line">					<span class="keyword">for</span> (<span class="keyword">int</span> q = len2 - <span class="number">1</span>; q &gt; <span class="number">1</span>; q--)  </span><br><span class="line">					&#123;</span><br><span class="line">						p[pindex++] = LL1[x][y][q];  <span class="comment">//将候选产生式倒着装入分析栈中</span></span><br><span class="line">						cmp.push(LL1[x][y][q]);  <span class="comment">//装入分析栈</span></span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span></span><br><span class="line">				&#123;</span><br><span class="line">					p[pindex] = <span class="string">&#x27;\0&#x27;</span>;  <span class="comment">//消除当前非终结符</span></span><br><span class="line">					pindex--;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">int</span> q = i; q &lt; len; q++)</span><br><span class="line">				&#123;</span><br><span class="line">					<span class="built_in">cout</span> &lt;&lt; str[q];    <span class="comment">//输出剩余输入串</span></span><br><span class="line">				&#125;</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;\t%c%s\n&quot;</span>, ch, LL1[x][y]);  <span class="comment">//输出所用到的产生式或匹配</span></span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				i = error(i, cnt, len, p, str);</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">				<span class="comment">///未找到，报错</span></span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span>  <span class="comment">//是终结符</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span> (ch == str[i])</span><br><span class="line">			&#123;</span><br><span class="line">				cmp.pop();   <span class="comment">//直接弹栈</span></span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;%d\t%s\t&quot;</span>, cnt, p);   <span class="comment">//输出该终结符</span></span><br><span class="line">				<span class="keyword">if</span> (ch == <span class="string">&#x27;#&#x27;</span>&amp;&amp;str[i] == <span class="string">&#x27;#&#x27;</span>)   <span class="comment">//直接接受并返回</span></span><br><span class="line">				&#123;</span><br><span class="line">					<span class="built_in">printf</span>(<span class="string">&quot;#\t接受\n&quot;</span>);</span><br><span class="line">					<span class="keyword">return</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">int</span> q = i; q &lt; len; q++)  <span class="comment">//输出剩余输入串的信息</span></span><br><span class="line">				&#123;</span><br><span class="line">					<span class="built_in">cout</span> &lt;&lt; str[q];</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;\t%c匹配\n&quot;</span>, ch);</span><br><span class="line">				pindex--;</span><br><span class="line">				p[pindex] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">				i++;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				i = error(i, cnt, len, p, str);</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">				<span class="comment">///报错</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		cnt++;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//输出follow和first集</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FF</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">string</span> fileName = <span class="string">&quot;productions.txt&quot;</span>;   <span class="comment">//包含文法规则的文本文件</span></span><br><span class="line">	<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt; productions; <span class="comment">//产生式集合，set会自动根据首字母的ASCII码进行从小到大的排序</span></span><br><span class="line">	<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt; split_productions; <span class="comment">//分解后的产生式集合 </span></span><br><span class="line">	<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt; Vt; <span class="comment">//终结符集合</span></span><br><span class="line">	<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt; Vn; <span class="comment">//非终结符集合</span></span><br><span class="line">	<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt; first; <span class="comment">//First集</span></span><br><span class="line">	<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt; follow; <span class="comment">//Follow集</span></span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span>; <span class="comment">//从文件读取产生式</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">splitProductions</span><span class="params">()</span></span>; <span class="comment">//分解产生式</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">findVtAndVn</span><span class="params">()</span></span>; <span class="comment">//获得终结符和非终结符</span></span><br><span class="line">	<span class="function"><span class="keyword">bool</span> <span class="title">isVn</span><span class="params">(<span class="built_in">string</span> s)</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">bool</span> <span class="title">isVt</span><span class="params">(<span class="built_in">string</span> s)</span></span>;</span><br><span class="line">	<span class="function"><span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt; <span class="title">getOneFirst</span><span class="params">(<span class="built_in">string</span> s)</span></span>; <span class="comment">//获得单个非终结符first集</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">getFirst</span><span class="params">()</span></span>; <span class="comment">//获得所有非终结符first集</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">getFollow</span><span class="params">()</span></span>; <span class="comment">//获得所有非终结符follow集</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">get_follow_again</span><span class="params">()</span></span>; <span class="comment">//求folloe集的步骤3（主要是位于文法末尾的非终结符）</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">FF::init</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="built_in">string</span> line;</span><br><span class="line">	<span class="function">ifstream <span class="title">in</span><span class="params">(fileName)</span></span>;</span><br><span class="line">	<span class="keyword">if</span> (in) &#123;</span><br><span class="line">		<span class="comment">//文法开始符号的follow集中放入$</span></span><br><span class="line">		getline(in, line);</span><br><span class="line">		productions.insert(line);  <span class="comment">//产生式集合加入第一行</span></span><br><span class="line">		follow[line.substr(<span class="number">0</span>, <span class="number">1</span>)].insert(<span class="string">&quot;$&quot;</span>);</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; line &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">while</span> (getline(in, line)) &#123;</span><br><span class="line">			productions.insert(line);</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; line &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">FF::splitProductions</span><span class="params">()</span> </span>&#123;  <span class="comment">//分割产生式</span></span><br><span class="line">	<span class="keyword">int</span> position = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator it = productions.begin(); it != productions.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">string</span> temp = *it;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; temp.length(); i++) &#123;</span><br><span class="line">			position = temp.find(<span class="string">&quot;-&gt;&quot;</span>);  <span class="comment">//找到-&gt;</span></span><br><span class="line">			<span class="built_in">string</span> s = temp.substr(<span class="number">0</span>, position);  <span class="comment">//截取-&gt;之前的</span></span><br><span class="line">			<span class="built_in">string</span> ss = temp.substr(position + <span class="number">2</span>);  <span class="comment">//截取-&gt;之后的</span></span><br><span class="line">			<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;sss;</span><br><span class="line">			<span class="built_in">string</span> t;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; ss.length(); j++) &#123;</span><br><span class="line">				<span class="keyword">if</span> (ss[j] == <span class="string">&#x27;|&#x27;</span>) &#123;   <span class="comment">//如果ss[j]==&#x27;|&#x27;，代表有新的产生式</span></span><br><span class="line">					sss.insert(t);</span><br><span class="line">					t = <span class="string">&quot;&quot;</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span></span><br><span class="line">				&#123;</span><br><span class="line">					t.append(ss.substr(j, <span class="number">1</span>));  </span><br><span class="line">				&#125;</span><br><span class="line"></span><br><span class="line">			&#125;</span><br><span class="line">			sss.insert(t);</span><br><span class="line">			split_productions.insert(<span class="built_in">pair</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt;(s, sss));  <span class="comment">//插入的一个非终结符对应的所有文法</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//输出一下分割的元素</span></span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt;::iterator it = split_productions.begin(); it != split_productions.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; it-&gt;first &lt;&lt; <span class="string">&quot;    &quot;</span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator ii = it-&gt;second.begin(); ii != it-&gt;second.end(); ii++) &#123;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; *ii &lt;&lt; <span class="string">&quot;    &quot;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">FF::findVtAndVn</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator it = productions.begin(); it != productions.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">string</span> temp = *it;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; temp.length(); i++) &#123;</span><br><span class="line">			<span class="keyword">if</span> (temp[i] == <span class="string">&#x27;-&#x27;</span> || temp[i] == <span class="string">&#x27;&gt;&#x27;</span> || temp[i] == <span class="string">&#x27;|&#x27;</span>)</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			<span class="comment">//是大写字母</span></span><br><span class="line">			<span class="keyword">if</span> (temp[i] &gt;= <span class="string">&#x27;A&#x27;</span> &amp;&amp; temp[i] &lt;= <span class="string">&#x27;Z&#x27;</span>) &#123;</span><br><span class="line">				<span class="comment">//后面带&#x27;</span></span><br><span class="line">				<span class="keyword">if</span> (temp[i + <span class="number">1</span>] == <span class="string">&#x27;\&#x27;&#x27;</span>) &#123;  <span class="comment">//这一步其实用不到，我把E&#x27;改成了M把T&#x27;改成了N</span></span><br><span class="line">					Vn.insert(temp.substr(i, <span class="number">2</span>));</span><br><span class="line">					i++;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span> &#123;</span><br><span class="line">					Vn.insert(temp.substr(i, <span class="number">1</span>));</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//是终结符</span></span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				Vt.insert(temp.substr(i, <span class="number">1</span>));   <span class="comment">//终结符直接插入</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;非终结符&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator it = Vn.begin(); it != Vn.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; *it &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;终结符&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator it = Vt.begin(); it != Vt.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; *it &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">FF::isVn</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (Vn.find(s) != Vn.end()) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">FF::isVt</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (Vt.find(s) != Vt.end()) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt; <span class="title">FF::getOneFirst</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;  <span class="comment">//该算法基于First集定义的递归查找思想实现</span></span><br><span class="line">	<span class="keyword">if</span> (split_productions.count(s) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">		<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;temp = split_productions[s];</span><br><span class="line">		<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator it = temp.begin(); it != temp.end(); it++) &#123;</span><br><span class="line">			<span class="built_in">string</span> stemp = *it;</span><br><span class="line">			<span class="keyword">if</span> (stemp == <span class="string">&quot;#&quot;</span>) &#123;</span><br><span class="line">				first[s].insert(<span class="string">&quot;#&quot;</span>);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				<span class="keyword">int</span> flagAll = <span class="number">0</span>; <span class="comment">//所有的非终结符的first集都有#；</span></span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; stemp.length(); i++) &#123;</span><br><span class="line">					<span class="keyword">int</span> flag = <span class="number">0</span>; <span class="comment">//当前的非终结符的first集有#；</span></span><br><span class="line">					<span class="keyword">if</span> (stemp[i + <span class="number">1</span>] == <span class="string">&#x27;\&#x27;&#x27;</span>) &#123;<span class="comment">//带&#x27;的非终结符</span></span><br><span class="line">						<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;t1 = getOneFirst(stemp.substr(i, <span class="number">2</span>));</span><br><span class="line">						<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator ii = t1.begin(); ii != t1.end(); ii++) &#123;</span><br><span class="line">							<span class="keyword">if</span> (*ii == <span class="string">&quot;#&quot;</span>) &#123;<span class="comment">//此时空串不可插入</span></span><br><span class="line">								flag = <span class="number">1</span>;</span><br><span class="line">							&#125;</span><br><span class="line">							<span class="keyword">else</span> &#123;</span><br><span class="line">								first[s].insert(*ii);</span><br><span class="line">							&#125;</span><br><span class="line">						&#125;</span><br><span class="line">						i++;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">else</span> <span class="keyword">if</span> (isVn(stemp.substr(i, <span class="number">1</span>)))<span class="comment">//单个非终结符</span></span><br><span class="line">					&#123;</span><br><span class="line">						<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;t2 = getOneFirst(stemp.substr(i, <span class="number">1</span>));   <span class="comment">//对于首字符还是非终结符的推导，按该非终结符继续递归</span></span><br><span class="line">						<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator ii = t2.begin(); ii != t2.end(); ii++) &#123;</span><br><span class="line">							<span class="keyword">if</span> (*ii == <span class="string">&quot;#&quot;</span>) &#123;<span class="comment">//此时空串不可插入</span></span><br><span class="line">								flag = <span class="number">1</span>;</span><br><span class="line">							&#125;</span><br><span class="line">							<span class="keyword">else</span> &#123;</span><br><span class="line">								first[s].insert(*ii);</span><br><span class="line">							&#125;</span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">else</span> &#123;<span class="comment">//终结符</span></span><br><span class="line">						first[s].insert(stemp.substr(i, <span class="number">1</span>));</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">if</span> (i == stemp.length() - <span class="number">1</span> &amp;&amp; flag == <span class="number">1</span>) &#123;</span><br><span class="line">						flagAll = <span class="number">1</span>;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">if</span> (flag == <span class="number">0</span>)</span><br><span class="line">						<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">if</span> (flagAll == <span class="number">1</span>) &#123;</span><br><span class="line">					first[s].insert(<span class="string">&quot;#&quot;</span>);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> first[s];</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">FF::getFirst</span><span class="params">()</span> </span>&#123;  <span class="comment">//按顺序输出first集内容</span></span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt;::iterator it = split_productions.begin(); it != split_productions.end(); it++) &#123;</span><br><span class="line">		getOneFirst(it-&gt;first);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;First集&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt;::iterator it = first.begin(); it != first.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; it-&gt;first &lt;&lt; <span class="string">&quot;:  &quot;</span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator ii = it-&gt;second.begin(); ii != it-&gt;second.end(); ii++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; *ii &lt;&lt; <span class="string">&quot;    &quot;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">FF::getFollow</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt;::iterator it = split_productions.begin(); it != split_productions.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">string</span> left = it-&gt;first;</span><br><span class="line">		<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;right = it-&gt;second;</span><br><span class="line">		<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator ii = right.begin(); ii != right.end(); ii++) &#123;</span><br><span class="line">			<span class="built_in">string</span> temp = *ii;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; temp.length(); i++) &#123;</span><br><span class="line">				<span class="keyword">if</span> (isVt(temp.substr(i, <span class="number">1</span>))) &#123;  <span class="comment">//终结符没有follow集，跳过</span></span><br><span class="line">					<span class="keyword">continue</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span> <span class="keyword">if</span> (i + <span class="number">1</span> &lt; temp.length() &amp;&amp; temp[i + <span class="number">1</span>] == <span class="string">&#x27;\&#x27;&#x27;</span>) &#123;<span class="comment">//带有’的非终结符</span></span><br><span class="line">					<span class="keyword">if</span> (isVt(temp.substr(i + <span class="number">2</span>, <span class="number">1</span>))) &#123;<span class="comment">//非终结符后面是终结符</span></span><br><span class="line">						follow[temp.substr(i, <span class="number">2</span>)].insert(temp.substr(i + <span class="number">2</span>, <span class="number">1</span>));</span><br><span class="line">						i++;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">else</span> &#123;<span class="comment">//非终结符后面是非终结符s</span></span><br><span class="line">						<span class="comment">//把后面非终结符的first集ff加入follow集中</span></span><br><span class="line">						<span class="built_in">string</span> s;</span><br><span class="line">						<span class="keyword">if</span> (i + <span class="number">3</span> &lt; temp.length() &amp;&amp; temp[i + <span class="number">3</span>] == <span class="string">&#x27;\&#x27;&#x27;</span>) &#123;</span><br><span class="line">							s = temp.substr(i + <span class="number">2</span>, <span class="number">2</span>);</span><br><span class="line">						&#125;</span><br><span class="line">						<span class="keyword">else</span> &#123;</span><br><span class="line">							s = temp.substr(i + <span class="number">2</span>, <span class="number">1</span>);</span><br><span class="line">						&#125;</span><br><span class="line">						<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt; ff = first[s];</span><br><span class="line">						<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator nn = ff.begin(); nn != ff.end(); nn++) &#123;</span><br><span class="line">							<span class="keyword">if</span> (*nn != <span class="string">&quot;#&quot;</span>)</span><br><span class="line">								follow[temp.substr(i, <span class="number">2</span>)].insert(*nn);</span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span> &#123;<span class="comment">//不带’的非终结符</span></span><br><span class="line"></span><br><span class="line">					<span class="keyword">if</span> (i + <span class="number">1</span> &lt; temp.length() &amp;&amp; isVt(temp.substr(i + <span class="number">1</span>, <span class="number">1</span>))) &#123;  <span class="comment">//非终结符后面是终结符</span></span><br><span class="line">						follow[temp.substr(i, <span class="number">1</span>)].insert(temp.substr(i + <span class="number">1</span>, <span class="number">1</span>));</span><br><span class="line">						i++;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">else</span> &#123;<span class="comment">//非终结符后面是非终结符s</span></span><br><span class="line">						<span class="comment">//把后面非终结符的first集ff加入follow集中</span></span><br><span class="line">						<span class="built_in">string</span> s;</span><br><span class="line">						<span class="keyword">if</span> (i + <span class="number">2</span> &lt; temp.length() &amp;&amp; temp[i + <span class="number">2</span>] == <span class="string">&#x27;\&#x27;&#x27;</span>) &#123;</span><br><span class="line">							s = temp.substr(i + <span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line">						&#125;</span><br><span class="line">						<span class="keyword">else</span> &#123;</span><br><span class="line">							s = temp.substr(i + <span class="number">1</span>, <span class="number">1</span>);   <span class="comment">//找到后面的非终结符</span></span><br><span class="line">						&#125;</span><br><span class="line">						<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt; ff = first[s];   <span class="comment">//把该非终结符的follow集加入</span></span><br><span class="line">						<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator nn = ff.begin(); nn != ff.end(); nn++) &#123;</span><br><span class="line">							<span class="keyword">if</span> (*nn != <span class="string">&quot;#&quot;</span>)</span><br><span class="line">								follow[temp.substr(i, <span class="number">1</span>)].insert(*nn);</span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//这一个需要多进行几次，因为follow是不断增长的</span></span><br><span class="line">	get_follow_again();</span><br><span class="line">	get_follow_again();</span><br><span class="line"></span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Follow集&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt;::iterator it = follow.begin(); it != follow.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; it-&gt;first &lt;&lt; <span class="string">&quot;:  &quot;</span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator ii = it-&gt;second.begin(); ii != it-&gt;second.end(); ii++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; *ii &lt;&lt; <span class="string">&quot;    &quot;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">FF::get_follow_again</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="built_in">map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;&gt;::iterator it = split_productions.begin(); it != split_productions.end(); it++) &#123;</span><br><span class="line">		<span class="built_in">string</span> left = it-&gt;first;</span><br><span class="line">		<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;right = it-&gt;second;</span><br><span class="line">		<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator ii = right.begin(); ii != right.end(); ii++) &#123;</span><br><span class="line">			<span class="built_in">string</span> temp = *ii;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> j = temp.length() - <span class="number">1</span>; j &gt; <span class="number">0</span>; j--) &#123;  <span class="comment">//倒着检查</span></span><br><span class="line">				<span class="built_in">string</span> now;</span><br><span class="line">				<span class="keyword">if</span> (temp[j] == <span class="string">&#x27;\&#x27;&#x27;</span>) &#123;</span><br><span class="line">					now = temp.substr(j - <span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line">					j--;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span> now = temp.substr(j, <span class="number">1</span>);</span><br><span class="line">				<span class="keyword">if</span> (isVt(now)) &#123;<span class="comment">//产生式最后是终结符</span></span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span> &#123;<span class="comment">//产生式最后是非终结符</span></span><br><span class="line">					<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;aa = follow[left];   <span class="comment">//最后是非终结符，需要把左部非终结符的部分放到这里。 </span></span><br><span class="line">					<span class="keyword">for</span> (<span class="built_in">set</span>&lt;<span class="built_in">string</span>&gt;::iterator pp = aa.begin(); pp != aa.end(); pp++) &#123;</span><br><span class="line">						follow[now].insert(*pp);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">if</span> (first[now].find(<span class="string">&quot;#&quot;</span>) == first[now].end())</span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//cout&lt;&lt;H[0]&lt;&lt;H[4]&lt;&lt;endl;</span></span><br><span class="line">	<span class="comment">//cout&lt;&lt;L[0]&lt;&lt;L[5]&lt;&lt;endl;</span></span><br><span class="line">	<span class="comment">/*for(int i = 0; i &lt; 5; i++)</span></span><br><span class="line"><span class="comment">	&#123;</span></span><br><span class="line"><span class="comment">		for(int j = 0 ; j &lt; 6; j++)</span></span><br><span class="line"><span class="comment">			printf(&quot;%5s&quot;,LL1[i][j]);</span></span><br><span class="line"><span class="comment">			cout&lt;&lt;endl;</span></span><br><span class="line"><span class="comment">	&#125;*/</span></span><br><span class="line">	</span><br><span class="line">	FF ff;</span><br><span class="line">	ff.init();</span><br><span class="line">	ff.splitProductions();</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	ff.findVtAndVn();</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	ff.getFirst();</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	ff.getFollow();</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;请输入将要被分析的句子：&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">char</span> str[<span class="number">200</span>];</span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; str;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="comment">//输出分析表（由于本程序采用直接输入已知分析表的形式，因此只需要直接输出分析表即可）</span></span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;LL(1)分析表：&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="function">ifstream <span class="title">infile</span><span class="params">(<span class="string">&quot;LL(1).txt&quot;</span>)</span></span>;</span><br><span class="line">	<span class="built_in">string</span> ans;</span><br><span class="line">	<span class="keyword">while</span> (infile.good())</span><br><span class="line">	&#123;</span><br><span class="line">		getline(infile, ans);</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; ans &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	infile.close();</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">int</span> len = <span class="built_in">strlen</span>(str);</span><br><span class="line">	cmp.push(<span class="string">&#x27;#&#x27;</span>);</span><br><span class="line">	cmp.push(<span class="string">&#x27;E&#x27;</span>);</span><br><span class="line">	analyze(str, len + <span class="number">1</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="1-5-总结"><a href="#1-5-总结" class="headerlink" title="1.5 总结"></a>1.5 总结</h1><p>在这之中我遇到最棘手的问题就是基本的数据结构搭建和first集合的求解算法，因为first集合要用到递归去求（递归真是永远的痛），虽然思路大概有，但是很久也没有敲出这个能运行的算法，最后还是靠着csdn上大佬的算法实现了这个功能。 </p>
<p>参考文献：<a target="_blank" rel="noopener" href="https://blog.csdn.net/wys5wys/article/details/85128052">(40条消息) 编译原理-实验二-LL(1)语法分析程序的设计_wys5的博客-CSDN博客_编译原理实验ll1语法分析</a> </p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>杨大熊
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://yang-da-xiong.gitee.io/2022/01/13/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86--%E5%AE%9E%E9%AA%8C2%20%E8%AF%AD%E6%B3%95%E5%88%86%E6%9E%90/" title="编译原理--实验2 语法分析">https://yang-da-xiong.gitee.io/2022/01/13/编译原理--实验2 语法分析/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/" rel="tag"># 编译原理</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/01/13/%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0%E9%A2%983----hdu1027%20%E2%80%9C%E6%9C%80%E5%B0%8F%E5%BA%8F%E5%88%97%E2%80%9C/" rel="prev" title="算法练习题3----hdu1027“最小序列”">
      <i class="fa fa-chevron-left"></i> 算法练习题3----hdu1027“最小序列”
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/01/14/%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0%E9%A2%984---%E8%93%9D%E6%A1%A5%E6%9D%AF2017%E7%9C%81%E8%B5%9B%E2%80%9C%E8%B4%AD%E7%89%A9%E5%8D%95%E2%80%9D/" rel="next" title="算法练习题4---蓝桥杯2017省赛“购物单”">
      算法练习题4---蓝桥杯2017省赛“购物单” <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-1%E5%AE%9E%E9%AA%8C%E7%9B%AE%E7%9A%84"><span class="nav-number">2.</span> <span class="nav-text">1.1实验目的</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-2-%E5%AE%9E%E9%AA%8C%E4%BB%BB%E5%8A%A1"><span class="nav-number">3.</span> <span class="nav-text">1.2 实验任务</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-3-%E5%AE%9E%E9%AA%8C%E5%86%85%E5%AE%B9"><span class="nav-number">4.</span> <span class="nav-text">1.3 实验内容</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-1-%E5%AE%9E%E9%AA%8C%E8%A6%81%E6%B1%82"><span class="nav-number">4.1.</span> <span class="nav-text">1.3.1 实验要求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-2-%E8%BE%93%E5%85%A5%E6%A0%BC%E5%BC%8F"><span class="nav-number">4.2.</span> <span class="nav-text">1.3.2 输入格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-3-%E8%BE%93%E5%87%BA%E6%A0%BC%E5%BC%8F"><span class="nav-number">4.3.</span> <span class="nav-text">1.3.3 输出格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-4-%E6%A0%B7%E4%BE%8B"><span class="nav-number">4.4.</span> <span class="nav-text">1.3.4 样例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-4-%E7%A8%8B%E5%BA%8F"><span class="nav-number">5.</span> <span class="nav-text">1.4 程序</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-1-%E7%A8%8B%E5%BA%8F%E6%B5%81%E7%A8%8B%E5%9B%BE"><span class="nav-number">5.1.</span> <span class="nav-text">1.4.1 程序流程图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-2-%E7%AE%97%E6%B3%95%E6%8F%8F%E8%BF%B0"><span class="nav-number">5.2.</span> <span class="nav-text">1.4.2 算法描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-3-%E7%A8%8B%E5%BA%8F%E6%BA%90%E7%A0%81"><span class="nav-number">5.3.</span> <span class="nav-text">1.4.3 程序源码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-5-%E6%80%BB%E7%BB%93"><span class="nav-number">6.</span> <span class="nav-text">1.5 总结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="杨大熊"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">杨大熊</p>
  <div class="site-description" itemprop="description">各类IT知识分享</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">58</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/Yang-Da-xiong?tab=repositories" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Yang-Da-xiong?tab&#x3D;repositories" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45775703?spm=1000.2115.3001.5343" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45775703?spm&#x3D;1000.2115.3001.5343" rel="noopener" target="_blank"><i class="fab fa-codiepie fa-fw"></i>CSDN</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">杨大熊</span>
</div>

<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>

<div class="theme-info">
<div class="powered-by"></div>
<span class="post-count">博客全站共56.5k字</span>
</div>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"log":false});</script></body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/schemes/clicklove.js"></script>
